package com.ericom.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity
public class Organization extends BaseObject {

	private Long id;
	private String name;
	private String description;
	private Set<ShippingMethod> shippingMethods = new HashSet<ShippingMethod>();
	private Set<PaymentMethod> paymentMethods=new HashSet<PaymentMethod>();
	
    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="org_payment",joinColumns={@JoinColumn(name="org_id")},inverseJoinColumns=@JoinColumn(name="payment_id"))
	public Set<PaymentMethod> getPaymentMethods() {
		return paymentMethods;
	}

	public void setPaymentMethods(Set<PaymentMethod> paymentMethods) {
		this.paymentMethods = paymentMethods;
	}
	public void addPaymentMethod(PaymentMethod paymentMethod){
		this.getPaymentMethods().add(paymentMethod);
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "org_ship", joinColumns = { @JoinColumn(name = "org_id") }, inverseJoinColumns = @JoinColumn(name = "ship_id"))
	public Set<ShippingMethod> getShippingMethods() {
		return shippingMethods;
	}

	public void setShippingMethods(Set<ShippingMethod> shippingMethods) {
		this.shippingMethods = shippingMethods;
	}

	public void addShippingMethod(ShippingMethod method) {
		this.getShippingMethods().add(method);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this).append("id", id).append("name", name)
				.append("description", description).toString();
	}

	@Override
	public boolean equals(final Object other) {
		if (!(other instanceof Organization))
			return false;
		Organization castOther = (Organization) other;
		return new EqualsBuilder().append(name, castOther.name)
				.append(description, castOther.description).isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(name).append(description)
				.toHashCode();
	}
}
